<?php
    require "../init.php";

    if(empty($_SESSION['admin'])){
        admin_redirect("请登录",3,ADMIN_ROOT_URL . "login.php");
        die;
    }

    $act = $_GET["act"];


    if($act == "edit"){
        //查看表单元素是否有空的
        if(empty($_POST['gname']) || empty($_POST['msg'])){
            admin_redirect("商品的信息不能为空");die;
        }
        //商品价格不能小于0
        if($_POST['price'] < 0){
            admin_redirect('商品价格不能小于0');die;
        }
        //获取传过来的数据
        $id = $_POST['id'];
        $gname = $_POST['gname'];
        $cate_id = $_POST['cate_id'];
        $price = $_POST['price'];
        $msg = htmlentities($_POST['msg']);


        //准备sql更改数据库
        $sql = "update ". PRE . "goods set gname = '" . $gname ."', cate_id = '" . $cate_id ."',price = '" . $price ."',msg = '" . $msg ."' where id = '" . $id ."'";

        //执行sql语句
        $result = execute($link, $sql);
        if($result){
            admin_redirect("修改商品信息成功",3,ADMIN_ROOT_URL . "goods/index.php");
            die;
        }else{
            admin_redirect("修改商品信息失败");
            die;
        }
    }elseif($act == "add"){
        //    验证表单数据是否为空
        /*
         * 1.执行图片上传
         *     图片上传失败,返回上级页面
         *
         *
         * 2.图片上传成功之后缩放
         *
         *          350*350
         *          150*150
         *          50*50
         *      缩放失败
         *      删除原图信息还有缩放图信息
         *
         * 3.先写入商品信息
         *      写入失败
         *          删除所有的图片信息
         *
         *
         *
         * 4.在写入商品图片信息
         *      写如失败删除所有商品信息
         *
         *
         *
         * */

        //查看表单元素是否有空的
        if(empty($_POST['gname']) || empty($_POST['price']) || empty($_POST['msg'])){
            admin_redirect("商品表单不能为空");
            die;
        }

        //判断图片是否上传
        if(empty($_FILES['gimg']['name'])){
            admin_redirect("商品图片不能为空");
            die;
        }



        // 上传图片保存路径
        $save_dir = ADMIN_ROOT_PATH . "../upload";
        //开始上传图片
        $file_name = uploadFile('gimg','10485760',array("image"),$save_dir);
        if(!$file_name){
            admin_redirect("上传图片失败");die;
        }
        //获取图片的名称
        $basename = basename($file_name);
        //开始缩放图片，如果有一张缩放失败，就删除所以缩放图片和刚上传的图片
        if(!zoomImg($file_name,350,350) || !zoomImg($file_name,250,250) || !zoomImg($file_name,150,150) || !zoomImg($file_name,50,50)){
            @unlink($file_name);
            @unlink(dirname($file_name)."/350_".$basename);
            @unlink(dirname($file_name)."/250_".$basename);
            @unlink(dirname($file_name)."/150_".$basename);
            @unlink(dirname($file_name)."/50_".$basename);
            admin_redirect("图片缩放失败");
            die;
        }

        //获得所有上传的信息 接受商品信息
        $gname = $_POST['gname'];   //商品名称
        $cate_id = $_POST['cate_id'];   //商品的分类id
        $price = $_POST['price'];   //商品的价格
        $is_new = $_POST['is_new']; //商品是否是新品
        $is_hot = $_POST['is_hot']; //商品是否热销
        $state = $_POST['state'];   //商品的状态,上架或下架
        $msg = htmlentities($_POST['msg']);   //商品信息

        //准备sql语句,向数据库中添加用户
        $sql = "insert into " . PRE . "goods (gname, cate_id, price, is_new, is_hot, state, msg,create_time) values
    ('$gname', '$cate_id', '$price', '$is_new', '$is_hot', '$state', '$msg', '".DATE."')";

        //执行sql语句  函数返回获取添加的商品的id
        $goods_id = execute($link, $sql);

        if(!$goods_id){
            //商品添加失败
            @unlink($file_name);
            @unlink(dirname($file_name)."/350_".$basename);
            @unlink(dirname($file_name)."/250_".$basename);
            @unlink(dirname($file_name)."/150_".$basename);
            @unlink(dirname($file_name)."/50_".$basename);
            admin_redirect("商品添加失败");
            die;
        }
        //写入图片信息
        $sql = "insert into " . PRE ."image (iname, goods_id, cover,create_time) values ('$basename', '$goods_id', 1, '".DATE."')";

        $result = execute($link, $sql);
        if(!$result){
            //写入图片失败
            @unlink($file_name);
            @unlink(dirname($file_name)."/350_".$basename);
            @unlink(dirname($file_name)."/250_".$basename);
            @unlink(dirname($file_name)."/150_".$basename);
            @unlink(dirname($file_name)."/50_".$basename);
            execute($link, "delete from " . PRE . "goods where goods_id = '$goods_id' ");
            admin_redirect("写入图片失败");
            die;

        }else{
            admin_redirect("商品添加成功",3,"./index.php");die;
        }
    }elseif ($act == "is_new"){
        //获取参数
        $id = $_GET['id'];
        $is_new = $_GET['is_new'] == 0 ? 1 : 0 ;

        //准备sql
        $sql = "update ". PRE . "goods set is_new=$is_new where id=$id";

        //执行sql
        $res = execute($link, $sql);
        if($res){
            header("location:".$_SERVER["HTTP_REFERER"]);
            die;
        }else{
            header("location:".$_SERVER["HTTP_REFERER"]);
            die;
        }
    }elseif ($act == "is_hot"){
        //获取参数
        $id = $_GET['id'];
        $is_hot = $_GET['is_hot'] == 0 ? 1 : 0 ;

        //准备sql
        $sql = "update ". PRE . "goods set is_hot=$is_hot where id=$id";

        //执行sql
        $res = execute($link, $sql);
        if($res){
            header("location:".$_SERVER["HTTP_REFERER"]);
            die;
        }else{
            header("location:".$_SERVER["HTTP_REFERER"]);
            die;
        }
    }elseif ($act == "state"){
        //获取参数
        $id = $_GET['id'];
        $state = $_GET['state'] == 0 ? 1 : 0 ;

        //准备sql
        $sql = "update ". PRE . "goods set state=$state where id=$id";

        //执行sql
        $res = execute($link, $sql);
        if($res){
            header("location:".$_SERVER["HTTP_REFERER"]);
            die;
        }else{
            header("location:".$_SERVER["HTTP_REFERER"]);
            die;
        }
    }elseif($act == "add_img"){
        // 上传图片保存路径
        $save_dir = ADMIN_ROOT_PATH . "../upload";
        //开始上传图片
        $file_name = uploadFile('gimg','10485760',array("image"),$save_dir);
        if(!$file_name){
            admin_redirect("上传图片失败");die;
        }
        //获取图片的名称
        $basename = basename($file_name);
        //开始缩放图片，如果有一张缩放失败，就删除所以缩放图片和刚上传的图片
        if(!zoomImg($file_name,350,350) || !zoomImg($file_name,250,250) || !zoomImg($file_name,150,150) || !zoomImg($file_name,50,50)){
            @unlink($file_name);
            @unlink(dirname($file_name)."/350_".$basename);
            @unlink(dirname($file_name)."/250_".$basename);
            @unlink(dirname($file_name)."/150_".$basename);
            @unlink(dirname($file_name)."/50_".$basename);
            admin_redirect("图片缩放失败");
            die;
        }
        //接受图片id
        $goods_id = $_POST['goods_id'];
        $sql = "insert into " . PRE ."image (iname,goods_id) values ('$basename','$goods_id')";
        $res = execute($link, $sql);
        if(!$res){
            @unlink($file_name);
            @unlink(dirname($file_name)."/350_".$basename);
            @unlink(dirname($file_name)."/250_".$basename);
            @unlink(dirname($file_name)."/150_".$basename);
            @unlink(dirname($file_name)."/50_".$basename);
            admin_redirect("写入图片失败");
            die;
        }else{
            admin_redirect("添加图片成功");
            die;
        }

    }elseif($act == "set_cover"){
        //得到商品id和图片id
        $goods_id = $_GET['goods_id'];
        $id = $_GET['id'];

        $sql = "update ". PRE . "image set cover = 0 where goods_id = '$goods_id' and cover = 1";
        $result = execute($link, $sql);

        if(!$result){
            admin_redirect("图片修改失败");die;
        }

        $sql = "update ". PRE . "image set cover = 1 where id = '$id'";
        $result = execute($link, $sql);

        if(!$result){
            admin_redirect("设置封面失败");die;
        }else{
            admin_redirect("设置封面成功");die;
        }

    }elseif($act == "del_img"){
        //得到图片id
        $id= $_GET['id'];

        //先判断该图片是否是封面,如果是,则不能删除
        $sql = "select id,iname from ". PRE ."image where id = '$id' and cover = 1";
        $row = getRow($link, $sql);
        if($row){
            admin_redirect("该图片是封面,不能删除");die;
        }
        //获得要删除的图片的名字
        $sql = "select iname from ". PRE ."image where id = '$id'";
        $iname = getRow($link, $sql);
        $iname = $iname['iname'];

        $sql = "delete from ".PRE ."image where id = '$id'";
        $res = execute($link, $sql);
        if($res){
            $file_name = get_img_url(ADMIN_ROOT_PATH.'../upload',$iname,0);

            @unlink($file_name);
            @unlink(dirname($file_name)."/350_".$iname);
            @unlink(dirname($file_name)."/250_".$iname);
            @unlink(dirname($file_name)."/150_".$iname);
            @unlink(dirname($file_name)."/50_".$iname);
            admin_redirect("图片删除成功");die;
        }else{
            admin_redirect("图片删除失败");die;
        }
    }




?>